package com.heima.comment.controller;

import com.heima.comment.service.CommentService;
import com.heima.model.common.dtos.CommentPageDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @Author Lijx
 * @Date 2024-08-01 10:48
 */
@RestController
@RequestMapping("/api/v1/comment")
@Slf4j
public class CommentController {


    @Autowired
    private CommentService commentService;

    /**
     * 发布评论
     * @param params
     * @return
     */
    @PostMapping("/save")
    public ResponseResult saveComment(@RequestBody Map<String,String> params){
        log.info("发表评论接口，入参={}",params);
        String articleId = params.get("articleId");
        String content = params.get("content");
        if (content == null || articleId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return commentService.saveComment(Long.valueOf(articleId),content);
    }


    /**
     * 展示文章评论列表
     * @param params
     * @return
     */
    @PostMapping("/load")
    public ResponseResult getCommentList(@RequestBody Map<String,Object> params){
        log.info("展示文章评论列表接口，入参={}",params);
        String articleId = (String) params.get("articleId");
        Object minDate = params.get("minDate");
        Integer index = (Integer)params.get("index");
        if (minDate == null || articleId == null || index == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return commentService.getCommentList(Long.valueOf(articleId),minDate,index);
    }

    /**
     * 文章评论点赞
     * @param params
     * @return
     */
    @PostMapping("/like")
    public ResponseResult commentlike(@RequestBody Map<String,String> params){
        log.info("文章评论点赞接口，入参={}",params);
        String commentId = params.get("commentId");
        String operation = params.get("operation");
        if (operation == null || commentId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return commentService.commentlike(commentId,Integer.valueOf(operation));
    }

}
